package com.frogs.ucenter.v1.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.frogs.framework.util.PageList;
import com.frogs.framework.util.StringUtil;
import com.frogs.system.util.TagStatusEnum;
import com.frogs.system.web.api.request.Request;
import com.frogs.system.web.api.response.Response;
import com.frogs.system.web.exception.ServiceOperationException;
import com.frogs.ucenter.dao.TagBaseInfoDao;
import com.frogs.ucenter.entity.TagBaseInfo;
import com.frogs.ucenter.v1.request.TagBaseInfoListRequest;
import com.frogs.ucenter.v1.response.ShowResponse;
import com.frogs.ucenter.v1.service.TagBaseInfoService;
import com.frogs.ucenter.v1.util.ApiConstants;

@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
@Service(ApiConstants.VERSION + "tagBaseInfoService")
public class TagBaseInfoServiceImpl implements TagBaseInfoService {
	Logger log = LoggerFactory.getLogger(TagBaseInfoServiceImpl.class);
	
	@Resource
	private TagBaseInfoDao tagBaseInfoDao;

	public TagBaseInfoServiceImpl() {
		super();
	}

	public TagBaseInfoDao getTagBaseInfoDao() {
		return tagBaseInfoDao;
	}

	@Override
	public Response listTags(Request request) throws Exception {
		try {
			TagBaseInfoListRequest tagListRequest = (TagBaseInfoListRequest) request;
			ShowResponse<Map<Integer,List<TagBaseInfo>>> response = new ShowResponse<Map<Integer,List<TagBaseInfo>>>();
			
			Map<String, Object> params = new HashMap<String, Object>();
			PageList<TagBaseInfo> tagBaseInfoList = new PageList<TagBaseInfo>();

			params.put("eqStatus", TagStatusEnum.TAG_STATUS_NOT_DELETE.getId());
			
			if(!StringUtil.isEmpty(tagListRequest.getParentTagType())) {
				params.put("inTagTypeDivList", tagListRequest.getParentTagType().split(","));
			}
			if(!StringUtil.isEmpty(tagListRequest.getTagType())) {
				params.put("inTagTypeList", tagListRequest.getTagType().split(","));
			}
			
			if(tagListRequest.getSex() != -1) {
				params.put("eqSex", tagListRequest.getSex());
			}
			
			if(tagListRequest.getAge() != -1) {
				params.put("btAge", tagListRequest.getAge());
			}
			
			tagBaseInfoList = tagBaseInfoDao.selectByMap(params, 1, 2000,false);
			response.setInfo(this.convertTagTypeListToMap(tagBaseInfoList));
			
			return response;
		} catch (Exception e) {
			log.error("func[{}] request[{}] exception[{}] desc[fail]",
					new Object[] { this.getClass().getName() +"."+Thread.currentThread().getStackTrace()[1].getMethodName(),
						request,Arrays.deepToString(e.getStackTrace()) });
			throw new ServiceOperationException("请求处理失败：" + e.getMessage());
		}
	}

	private Map<Integer,List<TagBaseInfo>> convertTagTypeListToMap(PageList<TagBaseInfo> tagBaseInfoList) {
		
		Map<Integer,List<TagBaseInfo>> map = new HashMap<Integer,List<TagBaseInfo>>();
		for (TagBaseInfo object : tagBaseInfoList) {
			
			if(map.get(object.getTagType()) == null){
				List<TagBaseInfo> list = new ArrayList<TagBaseInfo>();
				list.add(object);
				map.put(object.getTagType(), list);
			}else{
				List<TagBaseInfo> list = map.get(object.getTagType());
				list.add(object);
			}
		}
		
		return map;
	}

}